﻿using Entity; 
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

namespace ServiceCore.Tool.Text
{
    public interface IMchType_Helper
    {
        /// <summary>
        /// 获取所有等级信息
        /// </summary>
        /// <returns></returns>
        List<mch_type> GetStructure_Template();
        /// <summary>
        /// 指定某项获取等级信息
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        List<mch_type> GetStructure_Template(Expression<Func<mch_type, bool>> where);
        /// <summary>
        ///  写入等级信息
        /// </summary>
        /// <param name="TempInfo"></param>
        void WriteTemplate(List<mch_type> TempInfo);
    }
    public class MchType_Helper : IMchType_Helper
    {
        private string basePath = AppContext.BaseDirectory + "/wwwroot/Config/";
        /// <summary>
        /// 定义缓存中的模板列表
        /// </summary>
        public static List<mch_type> TempInfo = new List<mch_type>();
        public MchType_Helper() {

        }
        /// <summary>
        /// 进行一次文件初始化
        /// </summary>
        /// <returns></returns>
        public void Init()
        {
            if (!TempInfo.Any())
            {
                string TempList = ReadTemplate();
                try
                {
                    TempInfo = Newtonsoft.Json.JsonConvert.DeserializeObject<List<mch_type>>(TempList);
                }
                catch { }
                //return this;
            }
        }
        /// <summary>
        /// 进行一次文件初始化
        /// </summary>
        /// <returns></returns>
        public  void Ref()
        {

            string TempList = ReadTemplate();
            try
            {
                TempInfo = Newtonsoft.Json.JsonConvert.DeserializeObject<List<mch_type>>(TempList);
            }
            catch { }
        }
        /// <summary>
        /// 获取所有模板信息
        /// </summary>
        /// <returns></returns>
        public List<mch_type> GetStructure_Template()
        {
            Init();
            return TempInfo;
        }

        /// <summary>
        /// 获取模板信息
        /// </summary>
        /// <returns></returns>
        public List<mch_type> GetStructure_Template(Expression<Func<mch_type, bool>> where)
        {
            Init();
            return TempInfo.AsQueryable().Where(where).ToList();
        }
        /// <summary>
        /// 写入文件
        /// </summary>
        /// <param name="TempInfo"></param>
        public void WriteTemplate(List<mch_type> TempInfo)
        {
            try
            {
                //判断权限目录是否存在，如果不存在则开始创建       
                if (!Directory.Exists(basePath))
                {
                    Directory.CreateDirectory(basePath);
                }
                string File_Path = basePath + "/mch_type_Config.json";
                //创建指定内容文件
                using (FileStream fs = new FileStream(File_Path, FileMode.Create))
                {
                    StreamWriter sw = new StreamWriter(fs);
                    sw.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(TempInfo));
                    sw.Close();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally { Ref(); }
        } 
        /// <summary>
        /// 读取文件
        /// </summary>
        /// <param name="TempInfo"></param>
        private string ReadTemplate()
        {
            try
            {
                string File_Path = basePath + "/mch_type_Config.json";
                if (!File.Exists(File_Path))
                {
                    TemplateFileExist();
                }
                using (FileStream fS = new FileStream(File_Path, FileMode.Open, FileAccess.Read))
                {
                    StreamReader sr = new StreamReader(fS, Encoding.UTF8);//设定读写的编码                 
                    sr.BaseStream.Seek(0, SeekOrigin.Begin);
                    return sr.ReadToEnd();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        } 
        /// <summary>
        /// 如果文件不存在，则创建一个只有普通用户的默认等级
        /// </summary>
        private void TemplateFileExist()
        {
            string File_Path = basePath + "/mch_type_Config.json";
            if (!File.Exists(File_Path))
            {
                List<mch_type> NewTemp = new List<mch_type>();
                mch_type NewItem = new mch_type();
                NewTemp.Add(new mch_type()
                {
                    type_id = Entity.Mch_Type.IC,
                    type_name = "IC2",
                    kid_type_name = new string[] {
                     "IC2","MES","AC2","CC2","BY","PV","FE"
                }
                });
                NewTemp.Add(new mch_type()
                {
                    type_id = Entity.Mch_Type.IC2,
                    type_name = "IC",
                    kid_type_name = new string[] {
                       "IC","BF60","IC30","CW60","AC","CW10","CW30","AK"
                    }
                });
                NewTemp.Add(new mch_type()
                {
                    type_id = Entity.Mch_Type.Adult,
                    type_name = "格子柜",
                    kid_type_name = new string[] {
                       "F18","F21","F27","F36","F64"
                    }
                });
                NewTemp.Add(new mch_type()
                {
                    type_id = Entity.Mch_Type.Keystroke,
                    type_name = "按键机",
                    kid_type_name = new string[] {
                        "2LL701"
                    }
                });
                //直接按照自定义模板创造一个文件
                WriteTemplate(NewTemp);
            }
        }
    }
}
